/*
 * JOGRE (Java Online Gaming Real-time Engine) - Spades
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://jogre.sourceforge.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.jogre.mayi.client;

import java.util.ArrayList;
import java.util.List;

import nanoxml.XMLElement;

import org.jogre.client.IJogreModel;
import org.jogre.common.JogreModel;

/**
 * Spades model which extends the JogreModel contains a deck of cards.
 *
 * @author Garrett Lehman
 * @version Alpha 0.2.3
 */
public class MayIModel extends JogreModel {

	// Game states

	public static final int GAME_STATE_PRE_TURN = 1;
	public static final int GAME_STATE_PLAYING = 2;
	public static final int GAME_STATE_WAITING_TO_PLAY = 3;
	public static final int GAME_STATE_FINISHED_ROUND = 4;

	// Current round number
	private int round = 0;

	// Current game state for current player
	private int gameState = GAME_STATE_WAITING_TO_PLAY;

	// seat indexs (0,1,2,3 = bottom, left, top, right) assoc to seatNum
	private int[] seatIndex = null;
	
	private int players = -1;
	
	private List<String> mayIReceived = new ArrayList<String>();

	/**
	 * Default constructor
	 */
	public MayIModel(int players) {
		super(IJogreModel.GAME_TYPE_TURN_BASED);
		this.players = players;
		reset();
	}

	/**
	 * Reset all game variables
	 *
	 * @see org.jogre.common.JogreModel#reset()
	 */
	public void reset() {
		this.round = -1;
		this.seatIndex = null;

		resetRound();
	}

	/**
	 * Reset all round variables
	 */
	public void resetRound() {
		this.round++;
	}

	/**
	 * Set game state for current player
	 *
	 * @param gameState Game state defined by SpadesModel
	 */
	public void setGameState(int gameState) {
		this.gameState = gameState;
	}

	/**
	 * Get game state (defined by SpadesModel)
	 *
	 * @return game state (defined by SpadesModel)
	 */
	public int getGameState() {
		return this.gameState;
	}

	/**
	 * Checks whether seat indexes have been assigned (basically if all seat
	 * have been satten into)
	 *
	 * @return true if all seat have been indexed, false otherwise
	 */
	public boolean seatIndexesSet() {
		return this.seatIndex != null;
	}

	/**
	 * Get seat index by seat number. Seats are indexed by 0,1,2,3 <=>
	 * bottom,left,top,right. This was designed so the current player is always
	 * at the bottom position.
	 *
	 * @param seatNum
	 *            Seat number
	 * @return seat index by seat number
	 */
	public int getSeatIndex(int seatNum) {
		for (int i = 0; i < this.players; i++) {
			if (this.seatIndex[i] == seatNum)
				return i;
		}
		return -1;
	}

	/**
	 * Set seat indexes by providing the current player's seat number and
	 * setting indexes accordingly.
	 *
	 * @param seatNum
	 *            current player's seat number
	 */
	public void setSeatIndexes(int seatNum) {
		// set seat indexes from 0, 1, 2, 3 (bottom, left, top, left)
		this.seatIndex = new int[this.players];
		int i = 0;
		while (i < this.players) {
			this.seatIndex[i++] = seatNum++;
			if (seatNum >= this.players)
				seatNum = 0;
		}
	}

	public XMLElement flatten() {
		// FIXME FILL IN
		return null;
	}

	public void setState(XMLElement message) {
		// FIXME FILL IN		
	}

	public int getRound() {
		return round;
	}
	
	public void addMayIReceived(String username) {
		this.mayIReceived.add(username);
	}

	public List<String> getMayIReceived() {
		return mayIReceived;
	}
	
	public void resetMayIReceived() {
		this.mayIReceived.clear();
	}
	
	
}